package com.quanyan.travel.web;

import com.quanyan.api.APIResponse;
import com.quanyan.orderpay.response.mq.OrderNotifyMsg;
import com.quanyan.orderpay.response.mq.RefundNotifyMsg;
import com.quanyan.travel.service.TravelOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/travel/callback/")
public class CallbackController {

    @Autowired
    private TravelOrderService travelOrderService;

    private final static Logger logger = LoggerFactory.getLogger(CallbackController.class);


    /**
     * 支付成功回调
     *
     * */
    @RequestMapping(value="/payCallback", method = RequestMethod.POST)
    public APIResponse<?> openPayType(@RequestBody OrderNotifyMsg orderNotifyMsg)  {
        logger.info("支付回调开始,当前订单号: {}, 用户ID:{}", orderNotifyMsg.getOutTradeNo(), orderNotifyMsg.getUid());
        travelOrderService.payCallback(orderNotifyMsg);
        return APIResponse.returnSuccess();
    }

    /**
     * 退款回调
     *
     * */
    @RequestMapping(value="/refundCallback", method = RequestMethod.POST)
    public APIResponse<?> refundCallback(@RequestBody RefundNotifyMsg refundNotifyMsg)  {
        logger.info("退款回调,当前订单号: {}, 用户ID:{}", refundNotifyMsg.getOutTradeNo(), refundNotifyMsg.getUid());
        travelOrderService.refundCallback(refundNotifyMsg);
        return APIResponse.returnSuccess();
    }


    /**
     * 订单关闭回调
     *
     * */
    @RequestMapping(value="/orderCloseCallback", method = RequestMethod.POST)
    public APIResponse<?> orderCloseCallback(@RequestBody OrderNotifyMsg orderNotifyMsg)  {
        logger.info("订单系统,提示关闭订单,当前订单号: {}, 用户ID:{}", orderNotifyMsg.getOutTradeNo(), orderNotifyMsg.getUid());
        travelOrderService.closeOrderCallback(orderNotifyMsg.getOutTradeNo());
        return APIResponse.returnSuccess();
    }

    /**
     * 订单出现异常时,相关的回调
     *
     * */
    @RequestMapping(value="/orderExceptionCallback", method = RequestMethod.POST)
    public APIResponse<?> orderExceptionCallback(@RequestBody RefundNotifyMsg refundNotifyMsg)  {
        logger.info("订单系统,出现异常订单,进行订单状态的更新,当前订单号: {}, 用户ID:{}", refundNotifyMsg.getOutTradeNo(), refundNotifyMsg.getUid());
        travelOrderService.updateExceptionalOrderStatus(refundNotifyMsg.getOutTradeNo());
        return APIResponse.returnSuccess();
    }


}
